<template>

<el-form ref="queryFormRef" :inline="true" :model="queryForm" >
    <el-form-item style="width: 240px" label="菜单名称" prop="menuname" >
      <el-input v-model="queryForm.menuname" placeholder="menuame" />
    </el-form-item>
    <el-form-item  style="width: 240px" label="上级编号" prop="pid">
      <!-- <el-input v-model="queryForm.pid" placeholder="pid" clearable /> -->
      <el-select
        v-model="queryForm.pid"
        placeholder="请选择上级菜单"
      >
      <!-- 写死选项 不好
           动态数据 从数据库查 
      -->
        <el-option label="无" value="0" />
        <el-option v-for="option in optionList.optionData" :label="option.menuname" :value="option.mid" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="querySubmit">查询</el-button>
      <el-button  @click="resetQuery">重置</el-button>
      <el-button type="success"  @click="addOpen">添加</el-button>
      <el-button type="danger" @click="openDelete2">删除</el-button>
    </el-form-item>
  </el-form>

<!-- table中经常要搭配数据插槽 template 做数据优化 -->
   <el-table ref="tableRef" :data="tableList.tableData" height="500" style="width: 100%">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="mid" label="编号" />
      <el-table-column prop="menuname" label="名称" />
      <el-table-column prop="pid" label="上级菜单" >
         <template #default="scope">
            <el-tag v-if="scope.row.pid == 0" type="warning">{{scope.row.pid }}</el-tag>
            <el-tag v-else type="primary">{{scope.row.pid }}</el-tag>
         </template>
      </el-table-column>
      <el-table-column prop="pname" label="上级名称" >
         <template #default="scope">
            <el-tag v-if="scope.row.pid == 0" type="warning">{{scope.row.pname }}</el-tag>
            <el-tag v-else type="primary">{{scope.row.pname }}</el-tag>
         </template>
      </el-table-column>
      <el-table-column prop="show" label="是否启用" >
         <template #default="scope">
            <el-tag v-if="scope.row.show == 1" type="success">启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
         </template>
      </el-table-column>
      <el-table-column prop="url" label="访问地址" />
      <el-table-column prop="glyphicon" label="菜单图标" width="180">
         <template #default="scope">
            <el-tag type="primary">
               <el-icon><component :is="scope.row.glyphicon"></component></el-icon>
            </el-tag> 
            <el-tag type="primary">{{scope.row.glyphicon }}</el-tag>

         </template>
      </el-table-column>
      <el-table-column  label="操作" width="220">
         <template #default="scope">
            <el-button type="warning" @click="openEdit(scope.row.mid)">修改</el-button>
            <!-- <el-button type="danger" @click="openDelete(scope.row)">删除</el-button> -->
         </template>
      </el-table-column>
      
   </el-table>

   <el-pagination
      v-model:current-page="pageInfo.pageData.page"
      v-model:page-size="pageInfo.pageData.pageSize"
      :total="pageInfo.pageData.total"
      :page-sizes="[10, 20, 30]"
      layout="total, sizes, prev, pager, next, jumper"
      
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />


    <el-dialog
    v-model="addDialogVisible"
    title="添加"
    width="500"
  >
  <el-form ref="addFormRef" :model="addForm">
      <el-form-item label="菜单编号" :label-width="'100px'" prop="mid">
        <el-input v-model="addForm.mid" />
      </el-form-item>
      <el-form-item label="菜单名称" :label-width="'100px'" prop="menuname">
        <el-input v-model="addForm.menuname" />
      </el-form-item>
      <el-form-item label="上级编号" :label-width="'100px'" prop="pid">
        <!-- <el-input v-model="addForm.pid" /> -->
        <el-select
         v-model="addForm.pid"
         >
        <el-option label="无" value="0" />
        <el-option v-for="option in optionList.optionData" :label="option.menuname" :value="option.mid" />
      </el-select>


      </el-form-item>
      <el-form-item label="访问地址" :label-width="'100px'" prop="url">
        <el-input v-model="addForm.url" />
      </el-form-item>
      <el-form-item label="菜单图标" :label-width="'100px'" prop="glyphicon">
        <!-- <el-input v-model="addForm.glyphicon" /> -->
        <el-select v-model="addForm.glyphicon" >
        <el-option label="Connection" value="Connection" >
         <el-icon><Connection /></el-icon> Connection
        </el-option>
        <el-option label="Service" value="Service" >
         <el-icon><Service /></el-icon> Service
        </el-option>
        <el-option label="Ticket" value="Ticket" >
         <el-icon><Ticket /></el-icon> Ticket
        </el-option>
        <el-option label="Mic" value="Mic" >
         <el-icon><Mic /></el-icon> Mic
        </el-option>
        
      </el-select>

      </el-form-item>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addDialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="addSubmit">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>

  <el-dialog
    v-model="editDialogVisible"
    title="修改"
    width="500"
  >
  <el-form  :model="editForm.editData">
      <el-form-item label="菜单编号" :label-width="'100px'" prop="mid">
        <el-input disabled v-model="editForm.editData.mid" />
      </el-form-item>
      <el-form-item label="菜单名称" :label-width="'100px'" prop="menuname">
        <el-input v-model="editForm.editData.menuname" />
      </el-form-item>
      <el-form-item label="上级编号" :label-width="'100px'" prop="pid">
        <!-- <el-input v-model="addForm.pid" /> -->
        <el-select
         v-model="editForm.editData.pid"
         >
        <el-option label="无" :value="0" />
        <el-option v-for="option in optionList.optionData" :label="option.menuname" :value="option.mid" />
      </el-select>


      </el-form-item>
      <el-form-item label="访问地址" :label-width="'100px'" prop="url">
        <el-input v-model="editForm.editData.url" />
      </el-form-item>
      <el-form-item label="菜单图标" :label-width="'100px'" prop="glyphicon">
        <!-- <el-input v-model="addForm.glyphicon" /> -->
        <el-select v-model="editForm.editData.glyphicon" >
        <el-option label="Connection" value="Connection" >
         <el-icon><Connection /></el-icon> Connection
        </el-option>
        <el-option label="Service" value="Service" >
         <el-icon><Service /></el-icon> Service
        </el-option>
        <el-option label="Ticket" value="Ticket" >
         <el-icon><Ticket /></el-icon> Ticket
        </el-option>
        <el-option label="Mic" value="Mic" >
         <el-icon><Mic /></el-icon> Mic
        </el-option>
        
      </el-select>

      </el-form-item>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="editDialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="editFormSubmit">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>


</template>

<script setup>
import {ref,reactive,onMounted, nextTick} from 'vue'
import {get,post} from '@/myaxios'
import router from '@/router'
import {Edit,Histogram,SwitchButton} from '@element-plus/icons-vue'
import { ElMessage,ElMessageBox } from 'element-plus'
//多条删除
const tableRef = ref()

const openDelete2 = ()=>{
   let selectionRows =  tableRef.value.getSelectionRows();
   if(selectionRows.length>0){
      let paramMid = [];
   selectionRows.forEach((currentRow)=>{
      paramMid.push(currentRow.mid);
   })
   let paramMidStr = paramMid.join(',');
   console.log(paramMidStr);
   
   ElMessageBox.confirm(
    `确认要删除编号为:[${paramMidStr}]数据么`,
    '警告',
    {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
     console.log("确定删除");

      //准备提交参数
      let paramForm = {listMid:paramMidStr};
      //提交数据
      operationData("/menus/deleteByListMid",paramForm)
     
    })
    .catch(() => {
      console.log("取消删除");
     
    })
   }else{
      ElMessage.warning("请先选择数据....")
   }



}


//删除功能
const openDelete = (currentRow)=>{
   ElMessageBox.confirm(
    `确认要删除[${currentRow.mid}-${currentRow.menuname}]数据么`,
    '警告',
    {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
     console.log("确定删除");

      //准备提交参数
      let paramForm = {mid:currentRow.mid};
      //提交数据
      operationData("/menus/delete",paramForm)
     
    })
    .catch(() => {
      console.log("取消删除");
     
    })
}


//修改功能
//修改对话框bol值
const editDialogVisible = ref(false);
//修改表单对象
const editForm = reactive({editData:{
               mid:'',
               menuname:'',
               pid:'',
               url:'',
               glyphicon:''
            }
})

//打开修改框
const openEdit = async (mid)=>{
   //开框
   editDialogVisible.value = true
   //读取要修改的数据
   let resp = await get("/menus/getMenuForUpdate",{'mid':mid})
   console.log(resp);
   editForm.editData = resp.data.returnData

}
//修改提交
const editFormSubmit = async ()=>{
   editDialogVisible.value = false

   //准备提交参数
   let paramForm = editForm.editData;
   //提交数据
   operationData("/menus/update",paramForm)
}


//添加功能
//添加表单对象
const addForm = reactive({
   mid:'',
   menuname:'',
   pid:'',
   url:'',
   glyphicon:''
})



//添加框bol值
const addDialogVisible = ref(false)
//添加表单对象
const addFormRef = ref()

//打开添加对话框
const addOpen = ()=>{
   // 页面自动渲染
   //打开对话框
   addDialogVisible.value = true
   //框中的form对象不存在
   //vue机制问题 
   //通过nextTick() 在一次渲染后执行指定代码
   // nextTick(()=>{
   //    addFormRef.value.resetFields()
   // })
   //对象为空 不执行代码
   if(addFormRef.value!=undefined){
      addFormRef.value.resetFields()
   }
  

}
//提交添加表单
const addSubmit = async()=>{
   //template标签 写页面时 ref引用对象不用加value 但是js中要加
   //关闭对话框
   addDialogVisible.value = false
   //准备提交参数
   let paramForm = addForm;
   //提交数据
   operationData("/menus/add",paramForm)

} 


//查询功能
//下拉列表数组
const optionList = reactive({optionData:[]})

//查询表单对象
const queryFormRef = ref()

//查询条件表单
const queryForm = reactive({
   username:'1111',
   pid:''
})


//分页组件对应数据
const pageInfo = reactive({pageData:{
   page:1,
   pageSize:10,
   total:33
}})
//table对应数据列表
const tableList = reactive({tableData:[]})
//切换页码
const handleCurrentChange = async (val)=>{
   console.log(val);
   //准备参数
   pageInfo.pageData.page = val;
   let params = pageInfo.pageData;
   let params2 = queryForm;
   let allParam = {...params,...params2};
   //调用查询函数
   queryData(allParam)
}
//切换每页显示记录数
const handleSizeChange = async (val)=>{
   console.log(val);
    //准备参数
   pageInfo.pageData.page = 1;
   pageInfo.pageData.pageSize = val;
   let params = pageInfo.pageData;
   let params2 = queryForm;
   let allParam = {...params,...params2};
    //调用查询函数
   queryData(allParam)

   
}
//查询按钮提交
const querySubmit = ()=>{
   //准备参数
   let params = queryForm;
   // //调用查询函数
   queryData(params)

// let param1 = {name:'jack'};
// let param2 = {age:18};
// //合并json对象
// let allParam = {...param1,...param2};
// console.log(allParam);


}
//重置查询
const resetQuery = ()=>{
   //清空表单
   queryFormRef.value.resetFields()
   //按默认条件查询数据
   queryData({})
}


//公共函数
//查询函数
const queryData = async (params)=>{
   let resp =  await get("/menus/queryList",params);
  console.log(resp);
  //根据响应码做处理
  if(resp.data.returnCode ==  10000){
      tableList.tableData = resp.data.returnData;
      pageInfo.pageData = resp.data.pageInfo;
  }else if(resp.data.returnCode ==  10010){
      tableList.tableData = [];
      pageInfo.pageData = {page:1,pageSize:10,total:0};
  }
 
}
//菜单下拉列表
const queryOptions = async ()=>{
   let resp =  await get("/menus/querySelect",{});
   console.log(resp);
   optionList.optionData = resp.data.returnData
}
//操作数据
const operationData = async (url,paramForm)=>{
   //提交保存请求
   let resp = await post(url,paramForm);
   console.log(resp);
   //处理响应
   // 不要写成一个等号 resp.data.returnCode = 10020 结果时true
   if(resp.data.returnCode == 10020){
      ElMessage.success(resp.data.returnMsg);
   }else if(resp.data.returnCode ==10030){
      ElMessage.warning(resp.data.returnMsg);
   }else{
      ElMessage.error(resp.data.returnMsg);
   }
   //在当前页码和查询条件下刷新页面
   let params = pageInfo.pageData;
   let params2 = queryForm;
   let allParam = {...params,...params2};
   //调用查询函数
   queryData(allParam)
   //检查是否有关联数据要刷新
   queryOptions()

}


onMounted( async ()=>{
   queryData({})
   queryOptions()
})


</script>

<style scoped>

</style>